import json
from flask import request, render_template, make_response, jsonify
from flask_restx import Namespace, Resource, fields
from flask_restx.swagger import DEFAULT_RESPONSE_DESCRIPTION

import server.apis.test.api


ns = Namespace('test', description='test')


resource_fields = ns.model('Resource', {
    'interview_id': fields.String(description='面试编号', required=True),
    'name': fields.String(description='姓名', required=True),
    'age': fields.String(description='年龄'),
    'education': fields.String(description='学历'),
    'experience': fields.String(description='经验'),
    'habitation': fields.String(description='居住地'),
    'workplace': fields.String(description='工作地点'),
    'work_time': fields.String(description='工作时间'),
    'work_status': fields.String(description='工作状态'),
    'work_style': fields.String(description='工作形式', enum=['兼职', '全职']),
    'native': fields.String(description='原籍'),
    'commute_time': fields.String(description='通勤时间'),
    'job_intention': fields.String(description='工作意向'),
    'time_intention': fields.String(description='时间意向'),
    'subjective_evaluation': fields.String(description='主观评价')
})


parent = ns.model('Parent', {
    'name': fields.String,
    'age': fields.Integer(discriminator=True),
    'addr': fields.String(description='地址'),
    'gender': fields.String(description='性别', required=True, enum=['男', '女', '未知'])
})

child = ns.inherit('Child', parent, {
    'extra': fields.String
})


@ns.route("", strict_slashes=False)
class TestHandler(Resource):

    def get(self):
        page = request.args.get('offset', 1)
        page_size = request.args.get('limit', 100)
        res = server.apis.test.api.get_by_page(page, page_size)
        return make_response(render_template('index.html', data=res, offset=1, limit=20, count=100), 200)

    @ns.doc(model=resource_fields)
    def post(self):
        original_datas = json.loads(request.get_data(as_text=True))  # as_text=True 使得参数变成 Unicode 
        for original_data in original_datas["data"]:
            server.apis.test.api.add_or_update(**original_data)
        return make_response(jsonify(errmsg='success'), 201)

    @ns.doc(responses={403: 'Not Authorized'})
    def put(self):
        original_data = json.loads(request.get_data(as_text=True))
        try:
            interview_id = original_data.get('interview_id')
            data = original_data.get('data')
        except:
            return jsonify(code=401, errmsg='参数错误')
        
        server.apis.test.api.update(interview_id=interview_id, **data)
        return jsonify(code=200, errmsg='success')

    def delete(self):
        original_datas = json.loads(request.get_data(as_text=True))
        for i in original_datas.get("interview_ids"):
            server.apis.test.api.update(interview_id=i, is_delete=True)
        return jsonify(code=200, errmsg='success')

@ns.route("/<id>", strict_slashes=False)
@ns.doc(params={'id': 'An ID'})
class Test2Handler(Resource):

    def get(self, id):
        return "url 参数文档测试"
    
    @ns.doc(responses={403: 'Not Authorized'})
    def post(self, id):
        ns.abort(403)


@ns.route("/test3", strict_slashes=False)
class Test2Handler(Resource):

    @ns.marshal_with(child, as_list=True)  # 等价于 @api.marshal_list_with(resource_fields)
    def get(self):
        return "返回一个对象"
    
    @ns.marshal_with(child, code=201)  # 201 代表创建成功
    def post(self):
        return '创建一个对象成功', 201